// require()
import axios from "axios";

const ins: any = axios.create({
    baseURL: 'http://127.0.0.1:8880',
    timeout: 10000,
})

// http request 拦截器
ins.interceptors.request.use(
    (config: any) => {
        const token = localStorage.getItem('token');
        if (token) { // 判断是否存在token，如果存在的话，则每个http header都加上token
            config.headers.authorization = token; //请求头加上token
        }
        return config
    },
    (err: any) => {
        return Promise.reject(err)
    })


// http response 拦截器
ins.interceptors.response.use(
    (response: any) => {
        if (response.headers.authorization) {
            localStorage.setItem("token", response.headers.authorization);
        }
        return response
    },
    //接口错误状态处理，也就是说无响应时的处理
    (error: any) => {
        return Promise.reject(error.response.status) // 返回接口返回的错误信息
    })
export default ins;